import request from '@/utils/request'
// import authRoutes from '@/router/authRoutes'
// import router from '@/router'
import { defineStore } from "pinia";


// 数据持久化
let userInfo = localStorage.getItem('userInfo')
try{
    userInfo = JSON.parse(userInfo) || {}
}catch(err){
    userInfo = {}
}


export const useUserStore = defineStore('user',{
    state(){
        return {
            userInfo,
            // permissions:{}
        }
    },
    getters:{
        isLogin(state){
            return state.userInfo?.username
        }
    },
    actions:{
        async login(payload){
            const {data} = await request.get('/user/login',{
                params:payload
            })
            this.userInfo = data.data;
            localStorage.setItem('userInfo',JSON.stringify(data.data))

            // 获取用户权限列表
            // this.getPermission()

            return data;
        },
        logout(){
            this.userInfo = {}
            localStorage.removeItem('userInfo')
        },

        // 获取用户权限列表
        // async getPermission(){
        //     console.log('userInfo',this.userInfo)
        //     const {data} = await request.get('/admin/role',{
        //         params:{
        //             userid:this.userInfo._id
        //         }
        //     })
        //     console.log('permissions',data.data.role)
        //     this.permissions = data.data.role.permissions
        // }
    }
})

// userStore = useUserStore(); // 得到当前store实例
// userStore.userInfo
// userStore.login()
// userStore.logout()
// userStore.isLogin
// userStore.a++